#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Auto   : Administrator
@Date   : 2021/12/3
"""

import os
import re
import ssl
import json
import time
import requests
import threadpool
from requests.packages import urllib3
from requests.adapters import HTTPAdapter
from fake_useragent import UserAgent

requests.adapters.DEFAULT_RETRIES = 5
urllib3.disable_warnings()
UA = UserAgent()
HOME = os.getcwd()
JSON_PATH = os.path.join(HOME, "fulldata.json")
RESULT_PATH = os.path.join(HOME, "result3s.txt")
IP_REG = r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])'
HEADERS = {'Connection': 'close',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0'}
APP_HEADERS = {'Connection': 'close',
               'User-Agent': 'Mozilla/5.0 {} AppleWebKit/604.3.5 {} Version/13.0 MQQBrowser/10.1.1 Mobile/15B87 {}'.format(
                   '(iPhone; CPU iPhone OS 13_0 like Mac OS X)', '(KHTML, like Gecko)',
                   'Safari/604.1 QBWebViewUA/2 QBWebViewType/1 WKType/1')}
proxies = {'http': 'http://127.0.0.1:1080', 'https': 'http://127.0.0.1:1080', }

g_list = []

CIPHERS = (
    'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
    'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
    '!eNULL:!MD5'
)


class MyAdapter(HTTPAdapter):
    """
    A TransportAdapter that re-enables 3DES support in Requests.
    """

    def init_poolmanager(self, *args, **kwargs):
        context = urllib3.util.ssl_.create_urllib3_context(ciphers=CIPHERS)
        kwargs['ssl_context'] = context
        return super(MyAdapter, self).init_poolmanager(*args, **kwargs)

    def proxy_manager_for(self, *args, **kwargs):
        context = urllib3.util.ssl_.create_urllib3_context(ciphers=CIPHERS)
        kwargs['ssl_context'] = context
        return super(MyAdapter, self).proxy_manager_for(*args, **kwargs)


class TLSAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = urllib3.poolmanager.PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_version=ssl.PROTOCOL_TLSv1_1,
        )


def get_scan(j):
    """
    检测HTTP请求函数
    :param j:内容集合
    :return:返回消息
    """

    url = str(j[0]).replace(' ', '')
    has_port = False
    port = re.split("://", url)
    if len(port) == 2:
        if ":" not in port[1]:
            has_port = True
    else:
        if ":" not in url:
            has_port = True
    if has_port:
        url = url + ':' + j[4]
    if "http" not in url:
        url = "http://" + url
    with requests.Session() as sess:
        time.sleep(3)
        ssl._create_default_https_context = ssl._create_unverified_context
        sess.keep_alive = False  # 关闭多余连接
        try:
            # timeout:值将会用作 connect（连接）和 read（读取）二者的总和
            try:
                html = sess.get(url, headers=APP_HEADERS, proxies=proxies, stream=True, verify=False, timeout=(5, 5))
            except requests.exceptions.SSLError as se:
                sess.mount('https://', TLSAdapter())
                html = sess.get(url, headers=APP_HEADERS, proxies=proxies, stream=True, verify=False, timeout=(5, 5))
            res_code = html.status_code
            return "访问地址：{} ,返回请求状态码：{}".format(url, res_code)
        except Exception as ex:
            # print repr(ex)
            return "访问地址：{}, 网站失效, 异常信息：{}".format(url, ex.message)


def res_callback(request, result):
    """
    回调函数的结果保存到g_list数组中
    :param request:可以访问request.requestID
    :param result:执行结果
    :return:
    """
    if result:
        # print(request.requestID)
        print(result)
        g_list.append(result)
    else:
        pass


def thread_request(url_list):
    """
    线程池函数
    :param url_list:集合信息对象
    :return:
    """
    start_time = time.time()
    pool = threadpool.ThreadPool(10)  # 线程池数量
    reqs = threadpool.makeRequests(get_scan, url_list, res_callback)  # 使用线程池
    [pool.putRequest(req) for req in reqs]  # 简写 for req in reqs pool.putRequest(req)
    pool.wait()
    end_time = time.time()
    print("消耗时间: {}".format((end_time - start_time)))


def phishing_activity():
    with open(JSON_PATH, 'r') as f:
        json_loads = json.loads(f.read())

    thread_request(json_loads['results'])

    with open(RESULT_PATH, 'w') as res:
        print("开始写入结果文件中")
        for s in g_list:
            res.write("{} \n".format(s))


if __name__ == '__main__':
    # phishing_activity()
    # print(UA.ie)  # 随机打印ie浏览器任意版本
    # print(UA.firefox)  # 随机打印firefox浏览器任意版本
    # print(UA.chrome)  # 随机打印chrome浏览器任意版本
    # print(UA.random)  # 随机打印任意厂家的浏览器
    date = [
        "https://2138778.com",
        "昆仑国际",
        "154.221.25.244",
        "",
        "443",
        "HK",
        "",
        "",
        "Hong Kong",
        "",
        133115,
        "HK Kwaifong Group Limited",
        22.25,
        114.1667,
        "",
        "",
        ""
    ]
    print(get_scan(date))
